<?php

class ectools_mdl_office extends dbeav_model{
    
    /**
     * 得到默认包的信息
     * @params null
     * @return object servicename
     */
    public function get_package_info()
    {
        //return kernel::service('ectools_office.ectools_mdl_office');
	return null;
    }
    
    /**
     * 得到办事处名称
     * @params string office id
     * @return array local_name的数组
     */
    public function getById($officeId='')
    {
        //return $this->db->selectrow("select local_name from ".$this->table_name(1)." where office_id=".intval($officeId));
        return $this->dump(intval($officeId), 'local_name');
    }
    
    /**
     * 得到指定id的办事处信息
     * @params string office id
     * @return array 信息数组
     */
    public function getOfficeByParentId($parentId)
    {
        /*$sql="select office_id,local_name,p_office_id from ".$this->table_name(1)." where office_id=".intval($parentId);
        return $this->db->selectrow($sql);*/
        return $this->dump(intval($parentId), 'office_id,local_name,p_office_id');
    }
    
    /**
     * 指定office id的下级信息
     * @params int office id
     * @return array - 所有办事处数据数组
     */
    public function getAllChild($officeId)
    {
        /*
        $sql="select office_id from '.$this->table_name(1).' where p_office_id=".intval($officeId);
        $aTemp=$this->db->select($sql);
        if (is_array($aTemp)&&count($aTemp)>0){
            foreach($aTemp as $key => $val){
                $this->getAllChild($val['office_id']);
            }
        }
        $this->IdGroup[]=$officeId;   */
        unset($this->IdGroup);
        /*$sql = "select office_path from ".$this->table_name(1)." where office_id=".intval($officeId);
        $tmpRow=$this->db->selectrow($sql);*/
        $tmpRow = $this->dump(intval($officeId), 'office_path');
        $sql = "select office_id from ".$this->table_name(1)." where office_path like '%".$tmpRow['office_path']."%'";
        $row = $this->db->select($sql);
        
        if (is_array($row)&&count($row)>0)
        {
            foreach ($row as $key => $val)
            {
                $this->IdGroup[] = $val['office_id'];
            }
        }
        
        return $this->IdGroup;
    }
    
    /**
     * 得到指定office id同级的办事处信息
     * @params int office id
     * @return array 办事处信息
     */
    public function getGroupOfficeId($officeId)
    {
       //$row = $this->db->selectrow($sql='select office_path from '.$this->table_name(1).' where office_id='.intval($officeId));
       $row = $this->dump(intval($officeId), 'office_path');
       $path = $row['office_path'];
       $idGroup = array();
       
       $rows = $this->db->select($sql="select office_id from ".$this->table_name(1)." where office_path like '%".$path."%' and office_id<>".intval($officeId));
       if ($rows)
       {
           foreach ($rows as $key => $val)
           {
               $idGroup[] = $val['office_id'];
           }
       }
       
       return $idGroup;
    }
    
    /**
     * 得到指定office id的信息及父级的local_name
     * @params int office id
     * @return array
     */
    public function getDlAreaById($aOfficeId)
    {
        /*return $this->db->selectrow('SELECT * FROM sdb_dly_area WHERE area_id='.intval($aAreaId));*/
        $sql = 'select c.office_id,c.local_name,c.limited_amount,c.p_office_id,c.ordernum,p.local_name as parent_name from '.$this->table_name(1).' as c LEFT JOIN '.$this->table_name(1).' as p ON p.office_id=c.p_office_id where c.office_id='.intval($aOfficeId);
        
        return $this->db->selectrow($sql);
    }
    
    /**
     * 取指定office id对应的office id
     * @params string name 
     * @params int office id
     */
    public function checkDlArea($name,$p_office_id)
    {
        /*$aTemp = $this->db->selectrow("SELECT area_id FROM sdb_dly_area WHERE name='".$sName."' order by ordernum desc");
        return $aTemp['area_id']; */
        if ($p_office_id)
        {
            $aTemp = $this->dump(array('local_name' => $name, 'p_office_id' => $p_office_id), 'office_id');
        }
        else
        {
            $aTemp = $this->dump(array('local_name' => $name), 'office_id');
        }
        //$aTemp = $this->db->selectrow("SELECT office_id FROM ".$this->table_name(1)." WHERE local_name='".$name."' and p_office_id".($p_office_id?('='.intval($p_office_id)):' is null'));
        return $aTemp['office_id'];

    }

    function is_installed()
    {
        //$row = $this->db->selectrow('select count(*) as c from '.$this->table_name(1));
        //return $row['c']>0;
        $row = $this->count();
        return $row;
    }
    
    /**
     * 清除指定包名下的办事处信息
     * @params string 办事处包名
     */
    public function clearOldData($package='')
    {
        if ($package)
            $sql="delete from ".$this->table_name(1)." where package='".$package."'";
        else
            $sql="delete from ".$this->table_name(1)." where 1";
        
        $this->db->exec($sql);
    }
    
    public function change_office_data($ship_area='')
    {

        if (!$ship_area)
            return '';
        
        list($package,$office_name,$office_id) = explode(':',$ship_area);
        $arr_office_name = explode("/", $office_name);
        $arr_directory_name = array(
            app::get('ectools')->_('北京'),
            app::get('ectools')->_('天津'),
            app::get('ectools')->_('上海'),
            app::get('ectools')->_('重庆'),
        );

        if (!in_array($arr_office_name[0], $arr_directory_name))
        {
            return implode('',$arr_office_name);
            //return $arr_office_name[0].$arr_office_name[1].$arr_office_name[2];
        }
        else
        {
            unset($arr_office_name[0]);
            return implode('',$arr_office_name);
            //return $arr_office_name[1].$arr_office_name[2];
        }
    }
}
